﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using ClearCanvas.Dicom;
using System.IO;
using System.Xml.Linq;

namespace XMLGenerate
{
    public class XmlGenerate
    {
        private XDocument theDoc;
        private XElement rootElement;
        public XmlGenerate()
        {
            theDoc = new XDocument();
            rootElement = new XElement("ProwessXML");
            theDoc.Add(rootElement);
        }
        public void WriteXMLFile(String fileName)
        {

            theDoc.Save(fileName);
            theDoc = null;

        }
        public void WriteDicomAttributeCollection(XElement parentElement,DicomAttributeCollection attributes)
        {
            foreach (DicomAttribute att in attributes)
            {
                if (att is DicomAttributeSQ)
                {
                    XElement element = new XElement("Sequence");                    

                    XAttribute tag = new XAttribute("Tag",att.Tag.HexString);
                    
                    XAttribute tagName = new XAttribute("TagName",att.Tag.Name);
                    
                    element.Add(tag);
                    element.Add(tagName);                    

                    DicomSequenceItem[] items = (DicomSequenceItem[])att.Values;
                    foreach (DicomSequenceItem item in items)
                    {
                        XElement sequenceItem = new XElement("SequenceItem");

                        WriteDicomAttributeCollection(sequenceItem, item);

                        element.Add(sequenceItem);
                    }

                    if (parentElement == null)
                    {
                        rootElement.Add(element);
                    }
                    else
                    {
                        parentElement.Add(element);
                    }

                }
                else
                {
                    String value=string.Empty;
                    if (att.Values is String[])
                    {
                        value = GetArrayValue(att.Values as String[]);
                    }
                    else
                    {
                        value = att.GetString(0, string.Empty);
                    }

                    XElement element = new XElement("DicomTag",value);                    
                    
                    XAttribute tag = new XAttribute("Tag",att.Tag.HexString);
                    
                    XAttribute tagName = new XAttribute("TagName",att.Tag.Name);                                        
                    
                    element.Add(tag);
                    element.Add(tagName);                    

                    if (parentElement == null)
                    {
                        rootElement.Add(element);
                    }
                    else
                    {
                        parentElement.Add(element);
                    }
                }
            }
        }
        private string GetArrayValue(String[] arrayValue)
        {
            String result = string.Empty;
            if (arrayValue.Length > 1)
            {
                result = arrayValue[0];
                for (int i = 1; i < arrayValue.Length; i++)
                {
                    result += "\\";
                    result += arrayValue[i];
                }
            }
            else if (arrayValue.Length>0)
            {
                result = arrayValue[0];
            }            
            return result;

        }
    }
}
